//
// Created by wangjunjun on 2024/3/21.
//
/*
 * Created by wangjunjun on 2024/3/21.
 * @Author       : wangjunjun
 * @Date         : 2024-03-21
 * @brief        :ftp上传代码
 * @copyleft Apache 2.0
 * @version: V1.0.0
 */
#ifndef FTPUPLOAD_H
#define FTPUPLOAD_H
#include "monitor.h"
#include "httpresquest.h"
#include <memory>
#include <unistd.h>
#include <thread>
#include <mutex>
#include <string>
#include <curl/curl.h>
#include <csignal>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
class CFtpUpload{
public:
    //设置单例模式
    static std::shared_ptr<CFtpUpload>  GetInStance();
    void upLoadFiles(const fs::path& localFilePath, const std::string& remotePath);
    void Start(void);
private:
    CFtpUpload();
    ~CFtpUpload();
    void init(); //初始化获取配置
    static size_t read_callback(void* ptr, size_t size, size_t nmemb, void* stream);
    void uploadFileToServer(const std::string& localFilePath, const std::string& remoteFileName);
    void GetMitorStatus(void);
    static void handleSignal(int);
    //@brief:解析函数路径函数
    //param[in]: path
    //param[in]:requestBody
    void  parsePathFile(const std::string& path, RequestBody& requestBody);
    void removeEmptyDirectories(const std::string& directoryPath);
    //发送数据线程
    void DoSendDataProc(void);

private:
    static std::shared_ptr<CFtpUpload> m_InStance;
    static std::once_flag m_flag;

    std::string m_ftpUrl;
    std::string m_userName;
    std::string m_passWord;
    std::string m_address;
    std::string m_httpInsertUrl;
    std::thread*  m_pSendDataProc; //发送数据的线程
    std::thread*  m_pMonitorThread;
    int m_httpPort;
    int m_ftpPort;
    bool m_IsSend; //数据是否发送
    std::string m_localPath; //本地路径
    std::string m_remotePath;//远端路径

    std::mutex  m_Mtx; //加入共享锁
};


#endif //WLROBOTICS_FTPUPLOAD_H
